﻿
/* 
 * Since 2008 - 2013
 */
package com.hh.bss.sysadmin.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.*;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.report.vo.query.ReportQuery;
import com.hh.bss.sysadmin.dao.*;
import com.hh.bss.sysadmin.entity.*;
import com.hh.bss.sysadmin.service.*;
import com.hh.bss.sysadmin.vo.query.*;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class SystemUserReportConfController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	private static final String CONTENT_EXCEL_TYPE = "application/vnd.ms-excel";
	private static final ObjectMapper objectMapper = new ObjectMapper();
	
	private SystemUserReportConfService systemUserReportConfService;
	
	private SystemUserReportConf systemUserReportConf;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setSystemUserReportConfServiceImpl(SystemUserReportConfService service) {
		this.systemUserReportConfService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}



	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,SystemUserReportConfQuery systemUserReportConfQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			if (onlineUser!=null)systemUserReportConfQuery.setOnlineUser(onlineUser);
			
		}
		Page page = systemUserReportConfService.findPage(systemUserReportConfQuery);
		
		
		List<SystemUserReportConf> SystemUserReportConflist = (List) page.getResult();
		ListRange<SystemUserReportConf> resultList = new ListRange<SystemUserReportConf>();
		resultList.setRows(SystemUserReportConflist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}

	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,SystemUserReportConf systemUserReportConf) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			com.hh.bss.auth.client.OnlineUser onlineUser=null;
			if (onlineManager!=null){
				   onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(systemUserReportConf,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(systemUserReportConf,"createDt",new Date());}catch(Exception ex){}
				}

			if (systemUserReportConf.getId()!=null&&systemUserReportConf.getId().intValue()>0){
				SystemUserReportConf oldsystemUserReportConf = (SystemUserReportConf)systemUserReportConfService.getById(systemUserReportConf.getId());
				bind(request,oldsystemUserReportConf);
				if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldsystemUserReportConf,"modifiedDt",new Date());}catch(Exception ex){}
				}
				if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"modifier")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldsystemUserReportConf,"modifier",onlineUser.getUsername());}catch(Exception ex){}
				}
			    systemUserReportConfService.update(oldsystemUserReportConf);
				
			} else systemUserReportConfService.save(systemUserReportConf);
			result.put("success", true);
			result.put("msg", "保存成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	public void down(HttpServletRequest request,HttpServletResponse response,SystemUserReportConfQuery systemUserReportConfQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		OnlineManager onlineManager=super.getOnlineManager(request, response);
		if (onlineManager!=null){
			OnlineUser onlineUser=onlineManager.getOnlineUser();
			if (onlineUser!=null)systemUserReportConfQuery.setOnlineUser(onlineUser);
			
		}
		//Page page = accountReportService.workKpi(reportQuery);
		
		List list=systemUserReportConfService.findList(systemUserReportConfQuery);
		System.out.println("result:"+list.size());
		//
		try{
       		String title=new String(("员工指标设置.xls").getBytes("GBK"),"iso8859-1"); 
       		//HttpServletResponse response=ServletActionContext.getResponse();
       		response.setHeader("Content-Disposition", "attachment;filename=\""+title+"\"");
       		response.setContentType(CONTENT_EXCEL_TYPE);
       		ServletOutputStream os = response.getOutputStream();
       		Map titles=new LinkedHashMap();
       		//姓名	BD call	BD call完成	客户拜访	客户拜访完成	面试候选人	面试候选人完成	推荐候选人给客户	推荐候选人给客户完成	有效简历输入	有效简历输入完成	KPI 总分数
       		//'account','name','bdCallCount','bdCallCountFin','customerVisitCount','customerVisitCountFin','giCount','giCountFin','recomCount','recomCountFin','cvCount','cvCountFin'
       		titles.put("account", "员工");
       		titles.put("branchCompanyName", "分公司");
       		titles.put("deptName", "部门");
       		//ConstantDic.getConstantDicCategoryValue(ConstantDic.UserStatus, stau);
       		titles.put("titleName", "岗位");
       		titles.put("statusName", "状态");
       		//titles.put("bdCallCount", "BD call");
       		
       		titles.put("typeName", "配置类型");
       		titles.put("conf1", "Q1/BD CALL数(周)");
       		
       		titles.put("conf2", "Q2/VISIT(周)");
       		titles.put("conf3", "Q3/联系人选数量(周)");
       		titles.put("conf4", "Q4/内部推荐(周)");
       		
       	    
       		titles.put("conf5", "年度/顾问面试(周)");
       		titles.put("conf6", "外部推荐(周)");
       		titles.put("conf7", "客户面试(周)");
       		titles.put("conf8", "人选基本信息(周)");
       		titles.put("conf9", "人选简历(周)");
       		
       		
       		HSSFWorkbook wb = ExcelHelper.createExcelWorkbook("员工指标设置", list, titles);
       		wb.write(os);
       		os.flush();
       		os.close();  
       	}catch(Exception ex){
       		 ex.printStackTrace();
       	}
		
	}
	
	public void batchsave(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			String data_json =request.getParameter("data").toString();
            //设置创建用户和创建时间
            List l=objectMapper.readValue( data_json, new TypeReference<List<Map>>(){});
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			com.hh.bss.auth.client.OnlineUser onlineUser=null;
			
			
			SystemUserReportConf systemUserReportConf=null;
			//System.out.println(l);
			
			for(int i=0;i<l.size();i++){
				Map m=(Map)l.get(i);
				//System.out.println(m);
				systemUserReportConf=new SystemUserReportConf();
				systemUserReportConf.setId(StringHelper.parseInteger(ObjectUtils.toString(m.get("typeId")), 0));
                 systemUserReportConf.setType((Integer)m.get("type"));
				
				
				systemUserReportConf.setAccount((String)m.get("account"));
				boolean isNew=true;
				SystemUserReportConf oldsystemUserReportConf =null;
				if (systemUserReportConf.getId()!=null&&systemUserReportConf.getId().intValue()>0){
            		oldsystemUserReportConf = (SystemUserReportConf)systemUserReportConfService.getById(systemUserReportConf.getId());
            		if (oldsystemUserReportConf!=null){
            			systemUserReportConf=oldsystemUserReportConf;
            			isNew=false;
            		}
            		
				}else{
					oldsystemUserReportConf =systemUserReportConfService.getSystemUserReportConf(systemUserReportConf.getAccount(), systemUserReportConf.getType());
					if (oldsystemUserReportConf!=null){
            			systemUserReportConf=oldsystemUserReportConf;
            			isNew=false;
            		}
				}
				
				
				if (m.get("conf1")!=null)
				  systemUserReportConf.setConf1(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf1")), 0));
				else systemUserReportConf.setConf1(null);
				if (m.get("conf2")!=null)
					  systemUserReportConf.setConf2(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf2")), 0));
				else systemUserReportConf.setConf2(null);
				
				if (m.get("conf3")!=null)
					  systemUserReportConf.setConf3(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf3")), 0));
				else systemUserReportConf.setConf3(null);
				if (m.get("conf4")!=null)
					  systemUserReportConf.setConf4(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf4")), 0));
				else systemUserReportConf.setConf4(null);
				if (m.get("conf5")!=null)
					  systemUserReportConf.setConf5(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf5")), 0));
				else systemUserReportConf.setConf5(null);
				if (m.get("conf6")!=null)
					  systemUserReportConf.setConf6(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf6")), 0));
				else systemUserReportConf.setConf6(null);
				if (m.get("conf7")!=null)
					  systemUserReportConf.setConf7(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf7")), 0));
				else systemUserReportConf.setConf7(null);
				
				if (m.get("conf8")!=null)
					  systemUserReportConf.setConf8(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf8")), 0));
				else systemUserReportConf.setConf8(null);
				
				if (m.get("conf9")!=null)
					  systemUserReportConf.setConf9(StringHelper.parseDouble(ObjectUtils.toString(m.get("conf9")), 0));
				else systemUserReportConf.setConf9(null);
			
				//业绩则自动计算3700f				
				if (systemUserReportConf.getType()!=null&&systemUserReportConf.getType().intValue()==3700){
					
					if (m.get("conf1")!=null||m.get("conf2")!=null||m.get("conf3")!=null||m.get("conf4")!=null){
					double conf1=StringHelper.parseDouble(ObjectUtils.toString(m.get("conf1")), 0);
					double conf2=StringHelper.parseDouble(ObjectUtils.toString(m.get("conf2")), 0);
					double conf3=StringHelper.parseDouble(ObjectUtils.toString(m.get("conf3")), 0);
					double conf4=StringHelper.parseDouble(ObjectUtils.toString(m.get("conf4")), 0);
					systemUserReportConf.setConf5(new Double(conf1+conf2+conf3+conf4));
					}
					//systemUserReportConf.setConf5(null);
				}
				
				//-----------
			
				
				
	
				if (!isNew){
					//SystemUserReportConf oldsystemUserReportConf = (SystemUserReportConf)systemUserReportConfService.getById(systemUserReportConf.getId());
					
					//bind(request,oldsystemUserReportConf);
					if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"modifiedDt")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldsystemUserReportConf,"modifiedDt",new Date());}catch(Exception ex){}
					}
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldsystemUserReportConf,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
					System.out.println("---------"+systemUserReportConf);
				    systemUserReportConfService.update(systemUserReportConf);
					
				} else{
					if (onlineManager!=null){
						   onlineUser=onlineManager.getOnlineUser();
							if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"creator")) {
								try{com.hh.bss.util.BeanUtils.setProperty(systemUserReportConf,"creator",onlineUser.getUsername());}catch(Exception ex){}
							}
						}
									
					if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"createDt")) {
							try{com.hh.bss.util.BeanUtils.setProperty(systemUserReportConf,"createDt",new Date());}catch(Exception ex){}
						}
					systemUserReportConfService.save(systemUserReportConf);
				}
			
			}
			result.put("success", true);
			result.put("msg", "保存成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,SystemUserReportConf systemUserReportConf) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        SystemUserReportConf oldsystemUserReportConf = (SystemUserReportConf)systemUserReportConfService.getById(systemUserReportConf.getId());
                         
			bind(request,oldsystemUserReportConf);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldsystemUserReportConf,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemUserReportConf.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldsystemUserReportConf,"modifiedDt",new Date());}catch(Exception ex){}
				}


			systemUserReportConfService.update(oldsystemUserReportConf);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				systemUserReportConfService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
